<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class OwnserviceModelSettings extends JModel
{
	/**
	 * Settings data
	 *
	 * @var object
	*/
	var $_data = null;

	/**
	 * Constructor
	 *
	 * @since 1.0
	*/
	function __construct() {

		parent::__construct();
	}

	/**
	 * Method to get settings data
	 *
	 * @access public
	 * @return object
	*/
	function &getData(){

		$query = 'SELECT * FROM #__ownservice_settings ';
		
		$this->_db->setQuery($query);

		$this->_data = $this->_db->loadObjectList();

		return $this->_data;
	}

	function store($data){

		//Check the request forgeries
		JRequest::checkToken() or jexit('Invalid Token');

		$user = & JFactory::getUser();
		
		if($user->gid < 25) {
			if (!Ownservice::checkAccess('setting', 'edit', 'setting', 'all')) {
				JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
			}
		}

		$query ='TRUNCATE TABLE #__ownservice_settings';
		$this->_db->setQuery($query);
		if(!$this->_db->query()){
			$this->setError($this->_db->getErrorMsg());
			return false;
		}

		foreach($data as $key=>$value){
			$query="INSERT INTO #__ownservice_settings (title,value) VALUES(".$this->_db->Quote($key).",".$this->_db->Quote($value).")";
			$this->_db->setQuery($query);
			if(!$this->_db->query()){
				$this->setError($this->_db->getErrorMsg());
				return false;
			}
		}

		return true;
	}

	/**
	 * Method get user
	 *
	 * @return object
	 * @since 1.5
	*/
	function getUser($id) {

		$query = ' SELECT p.*, u.name FROM #__ownservice_users AS p '
				.' LEFT JOIN #__users AS u ON u.id=p.user_id '
				.' WHERE u.id='.$id.' AND u.block = 0 ';
		$this->_db->setQuery($query);
		$profile = $this->_db->loadObject();

		return $profile;
	}
}